<template>
	<view class="containerText">
		<!-- 		<up-textarea v-model="_value" :placeholder="placeholder" count :disabled="disabled" :customStyle="{width: '90vw'}"
			border="surround" auto-height ></up-textarea> -->
		<textarea ref="textarea" auto-height v-model="_value" :placeholder="placeholder" placeholder-style="color:rgb(192, 196, 204)"
			:disabled="!disabled" @linechange="onLineChange" />

	</view>
</template>

<script>
	import {
  useUserStore
} from '@/pinia-store/user'
import {
  mapStores
} from 'pinia'
import componentMinxins from '../ComponentMinxins'

	export default {
		mixins: [componentMinxins],
		name: "TextareaInput",
		components: {},
		props: {
			itemIndex: {
				type: Number,
				default: 0
			},
			value: {
				type: String,
				default: null
			},
			placeholder: {
				type: String,
				default: '请输入内容'
			},
			disabled: {
				type: Boolean,
				default: false
			},
			title:{
				type: String,
				default: null
			},
      titleItems:{
        type: String,
        default: null
      },
      ApplyDetailList: {
        type: Array,
        default: () => {
          return []
        }
      },
      itemId: {
        type: String,
        required: true
      },
		},
		watch: {
			_value: {
				handler(newValue) {
					let value = uni.getStorageSync('formData');
					let formItems = JSON.parse(value.formItems);
					formItems.forEach(item => {
						if(item.title == this.title){
							item.value = newValue;
							uni.setStorageSync(item.id, newValue);
						}else if(this.titleItems == item.title && item.title == this.titleItems){
              item.props.items.forEach(item =>{
                if (item.title == this.title) {
                  item.value = newValue;
                  uni.setStorageSync(item.id, newValue);
                }
              })

            }
					})
				}
			}
		},
		data() {
			return {
				_value: '',
			}
		},
		onReady() {

		},
		computed: {
			...mapStores(useUserStore)
		},
    created(){
      let value = uni.getStorageSync('formData');
      let formItems = JSON.parse(value.formItems);
      formItems.forEach(item => {
        if (item.title == this.title) {
          uni.removeStorageSync(`${item.id}`);
        }
      })
      this.setechodata()
    },
		mounted() {

		},
		methods: {
      setechodata(){
        // 添加数据回显逻辑
        if (this.ApplyDetailList && this.ApplyDetailList.formData  && this.itemId)  {
          // 检查 formData 中是否存在当前 itemId 对应的字段 
          if (this.ApplyDetailList.formData.hasOwnProperty(this.itemId))  {
            // 获取对应的值
            const fieldValue = this.ApplyDetailList.formData[this.itemId]; 
            
            // 如果值是数组，取第一个元素（根据你的数据结构）
            if (Array.isArray(fieldValue)  && fieldValue.length  > 0) {
              this._value = fieldValue
            } else {
              // 直接赋值 
              this._value = fieldValue || '';
            }
          }
        }
      },
			//监听textarea行数变化
			onLineChange(e) {
				this.$emit('height-change', 1);
			}
		}
	}
</script>

<style>
 .containerText{
   background-color: #ffffff !important;
 }
</style>
